<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>日期/时间算法</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="datetime.examples.html">« 范例</a></li>
      <li style="float: right;"><a href="class.datetime.html">DateTime »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="datetime.examples.html">范例</a></li>
    <li>日期/时间算法</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="datetime.examples-arithmetic" class="section">
  <h2 class="title">日期/时间算法</h2>
  <p class="para">
   以下示例展示了有关 DST 转换和具有不同天数的月份的日期/时间算法的一些缺陷。
  </p>
  <p class="para">
   <div class="example" id="example-1993">
    <p><strong>示例 #1 DateTimeImmutable::add/sub 添加包含经过时间的间隔</strong></p>
    <div class="example-contents"><p>
     在 DST 转换上添加 PT24H 似乎会增加 23/25 小时（大多数时区）。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dt&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">"2015-11-01&nbsp;00:00:00"</span><span style="color: #007700">,&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeZone</span><span style="color: #007700">(</span><span style="color: #DD0000">"America/New_York"</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">"Start:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i:s&nbsp;P"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$dt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">"PT3H"</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">"End:&nbsp;&nbsp;&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i:s&nbsp;P"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Start: 2015-11-01 00:00:00 -04:00
End:   2015-11-01 02:00:00 -05:00
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-1994">
    <p><strong>示例 #2 DateTimeImmutable::modify 和 strtotime 递增/减单个组件值</strong></p>
    <div class="example-contents"><p>
     在 DST 转换上添加 +24 小时将准确添加 24 小时，如日期/时间字符串中所示（除非开始/结束时间位于转换点）。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dt&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">"2015-11-01&nbsp;00:00:00"</span><span style="color: #007700">,&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeZone</span><span style="color: #007700">(</span><span style="color: #DD0000">"America/New_York"</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">"Start:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i:s&nbsp;P"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$dt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">modify</span><span style="color: #007700">(</span><span style="color: #DD0000">"+24&nbsp;hours"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"End:&nbsp;&nbsp;&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i:s&nbsp;P"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Start: 2015-11-01 00:00:00 -04:00
End:   2015-11-02 00:00:00 -05:00
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-1995">
    <p><strong>示例 #3 新增/减去时间可能会溢出/下溢日期</strong></p>
    <div class="example-contents"><p>
     就像 1 月 31 日 + 1 月会导致 3 月 2 日（闰年）或 3 月 3 日（平年）。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Normal&nbsp;year:\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;2&nbsp;月有&nbsp;28&nbsp;天<br /></span><span style="color: #0000BB">$dt&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">"2015-01-31&nbsp;00:00:00"</span><span style="color: #007700">,&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeZone</span><span style="color: #007700">(</span><span style="color: #DD0000">"America/New_York"</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">"Start:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i:s&nbsp;P"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$dt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">modify</span><span style="color: #007700">(</span><span style="color: #DD0000">"+1&nbsp;month"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"End:&nbsp;&nbsp;&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i:s&nbsp;P"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Leap&nbsp;year:\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;2&nbsp;月有&nbsp;29&nbsp;天<br /></span><span style="color: #0000BB">$dt&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">"2016-01-31&nbsp;00:00:00"</span><span style="color: #007700">,&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeZone</span><span style="color: #007700">(</span><span style="color: #DD0000">"America/New_York"</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">"Start:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i:s&nbsp;P"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$dt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">modify</span><span style="color: #007700">(</span><span style="color: #DD0000">"+1&nbsp;month"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"End:&nbsp;&nbsp;&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i:s&nbsp;P"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Normal year:
Start: 2015-01-31 00:00:00 -05:00
End:   2015-03-03 00:00:00 -05:00
Leap year:
Start: 2016-01-31 00:00:00 -05:00
End:   2016-03-02 00:00:00 -05:00
</pre></div>
    </div>
    <div class="example-contents"><p>
     要获取下个月的最后一天（例如防止溢出），可以使用 <code class="literal">last day of</code> 格式。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Normal&nbsp;year:\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;2&nbsp;月有&nbsp;28&nbsp;天<br /></span><span style="color: #0000BB">$dt&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">"2015-01-31&nbsp;00:00:00"</span><span style="color: #007700">,&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeZone</span><span style="color: #007700">(</span><span style="color: #DD0000">"America/New_York"</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">"Start:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i:s&nbsp;P"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$dt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">modify</span><span style="color: #007700">(</span><span style="color: #DD0000">"last&nbsp;day&nbsp;of&nbsp;next&nbsp;month"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"End:&nbsp;&nbsp;&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i:s&nbsp;P"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Leap&nbsp;year:\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;2&nbsp;月有&nbsp;29&nbsp;天<br /></span><span style="color: #0000BB">$dt&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">"2016-01-31&nbsp;00:00:00"</span><span style="color: #007700">,&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeZone</span><span style="color: #007700">(</span><span style="color: #DD0000">"America/New_York"</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">"Start:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i:s&nbsp;P"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$dt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">modify</span><span style="color: #007700">(</span><span style="color: #DD0000">"last&nbsp;day&nbsp;of&nbsp;next&nbsp;month"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"End:&nbsp;&nbsp;&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i:s&nbsp;P"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Normal year:
Start: 2015-01-31 00:00:00 -05:00
End:   2015-02-28 00:00:00 -05:00
Leap year:
Start: 2016-01-31 00:00:00 -05:00
End:   2016-02-29 00:00:00 -05:00
</pre></div>
    </div>
   </div>
  </p>
 </div></div></div></body></html>